<?php
namespace newMobile\Controller;
use Think\Controller;
class CrowdfundingController extends CommonController {

    public function index(){

        $this->display();
    }
    public function ajax_crowdfunding(){
        $page=I('get.page');
        $num=2;
        $strat=($page-1)*$num;
        $html='';
        $crowdfunding=M('crowdfunding');
        $count=$crowdfunding->field('yt_shop.id,yt_shop.title,yt_shop.image,yt_crowdfunding.*')
            ->join('yt_shop on yt_shop.id=yt_crowdfunding.goods_id')
            ->where("yt_crowdfunding.end_time>='".date('Y-m-d H:i:s',time())."' and yt_crowdfunding.start_time<='".date('Y-m-d H:i:s',time())."'and yt_shop.ishidden='0'")
            ->order('yt_crowdfunding.create_time DESC')
            ->count();
        $crowdfundinglist=$crowdfunding->field('yt_shop.title,yt_shop.image,yt_crowdfunding.*')
            ->join('yt_shop on yt_shop.id=yt_crowdfunding.goods_id')
            ->where("yt_crowdfunding.end_time>='".date('Y-m-d H:i:s',time())."' and yt_crowdfunding.start_time<='".date('Y-m-d H:i:s',time())."'and yt_shop.ishidden='0'")
            ->order('yt_crowdfunding.create_time DESC')
            ->limit($strat,$num)
            ->select();
        //print_r($crowdfundinglist);
        $page=ceil($count/$num);
         foreach($crowdfundinglist as $key=>$value){
             //print_r('1111');
             $endtime=$this->ShengYu_Tian_Shi_Fen(strtotime($value['end_time']));
             //print_r($endtime);
            $html=$html.'<div class="article-item">
						<a href="'.U('shop/index',array('id'=>$value['goods_id'])).'">
							<div class="article-content">
								<img src="'.$value['image'].'">
								<div class="article-title ">
									<div class="article-title-footer">
										<div class="article-footer-lf">
										   <div class="article-icons like-btn" data-refer-id="92">
						            <i class="icon-article-unlike " style="margin-bottom:.1rem"></i>
						            <span class="liken-num"> 剩余'.$endtime.'</span>
					            </div>
										</div>
										<div class="article-footer-rg">
											<div class="article-icons">
												<i class="icon-read"></i>
												<span class="read-num">'.$value['num'].'</span>
											</div>
										</div>
									</div>
								</div>
								
							</div>
							<div class="article-footer">
								<p>'.$value['project_name'].'</p>
								<p class="b-price">								
								<em>已筹</em>'.$value['now_money'].'元
							</p>
							</div>
						</a>
				  </div>';
        }
        $data['content']=$html;
        $data['pages']=$page;
        $this->ajaxReturn($data);




    }


    /*function details(){
        $id=I('get.id');
        $crowdfunding=M('crowdfunding');
        $crowdfunding_info=$crowdfunding->where("id=$id")->find();
        $goods_id=$crowdfunding_info['goods_id'];
        $shop=M('shop')->find($goods_id);
        $shop_content=M('shop_content')->where('shop_id='.$goods_id)->find();
        M('shop_content')->where('shop_id='.$goods_id)->setInc('story_hits');
        $store_template=M('store_template')->where('store_id='.$shop['store_id'])->find();
        $store_info=M('store')->find($shop['store_id']);
        $this->assign('store_template',$store_template);
        $this->assign('store_info',$store_info);
        $this->assign('shop_content',$shop_content);
        $this->assign('shop',$shop);
        $this->assign('crowdfunding_info',$crowdfunding_info);
        $this->display();
    }*/
    public function details(){

        $id=I('get.id');
        $crowdfunding=M('crowdfunding');
        $crowdfunding_info=$crowdfunding->where("id=$id")->find();
        $goods_id=$crowdfunding_info['goods_id'];
        $crowdfunding_info['surplus_time']=$this->ShengYu_Tian_Shi_Fen(strtotime($crowdfunding_info['end_time']));
        $crowdfunding_info['percent']=round( $crowdfunding_info['now_money']/$crowdfunding_info['total_money'] * 100 , 2);
        //print_r($crowdfunding_info);
        //足迹
        $isweixin=is_weixin();
        if($isweixin){
            $appid=C('weixin.APP_KEY');
            $appsecret=C('weixin.APP_SECRET');
            $weObj = new \Org\Util\Jssdk($appid,$appsecret);
            $wechat=$weObj->GetSignPackage();
            $this->assign('signPackage',$wechat);
        }

        if(session('user_id')){
            $history['shop_id']=$goods_id;
            $history['user_id']=session('user_id');
            $users_shophistory=M('users_shophistory');
            $rs_history=$users_shophistory->where($history)->select();
            if($rs_history){
                $users_shophistory->where($history)->delete();
            }else{
                $history['addtime']=time();
                $users_shophistory->add($history);
            }
        }
        $shop=M('shop');
        $shop_info=$shop->find($goods_id);
        if(!$shop_info){
            $this->error('该产品不存在！');
        }
        if($shop_info['ishidden']>0){
            $this->error('该产品已下架！');
        }


        if($_GET['rec_id']){
            $rec_id=$_GET['rec_id'];
            //注释后自己可以领自己的抵扣券
//    		if($rec_id==session('user_id')){
//				$this->redirect('shop/index', array('id' =>$goods_id));
//    		}
//		 	$rs=M('order')
//		 		->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
//		 		->where("yt_order_goods.goods_id=$goods_id and yt_order.order_state='40' and yt_order.refund_state='0' and yt_order.buyer_id=$rec_id ")
//		 		->find();
            $shop_url=U('shop/index', array('id' =>$goods_id));
            $rs=get_daiyan_status($goods_id,$rec_id);
            if(!$rs){
                $this->error('该用户无代言该产品资格！',$shop_url);
            }else{
                if(session('user_id')){
                    $daiyan_info['uid']=session('user_id');
                    $daiyan_info['bid']=$_GET['rec_id'];
                    $daiyan_info['shop_id']=$goods_id;
                    $daiyan_info['time']=time();

                    $invoice=get_web_invoice();

                    $start_time=time()-60*60*24*$invoice['web_diyong'];
                    $uid=session('user_id');
                    $daiyan=M('daiyan')->where("bid=$rec_id and shop_id=$goods_id and time>$start_time and uid=$uid ")->find();

                    if(!$daiyan){
                        $daiyan_rs=M('daiyan')->add($daiyan_info);
                        $buser_id=session('user_id');
                        $buser_info=M('users')->find($buser_id);
                        if($buser_info['wx_h5openid']){
                            $keyword1='58代言'.$shop_info['dikouquan'].'元现金抵扣券';
                            $keyword2=date('Y-m-d H:i:s',time());
                            send_info_dikouquan($buser_info['wx_h5openid'],$keyword1,$keyword2);
                        }
                        if($daiyan_rs){
                            $this->assign('daiyan_rs',1);
                        }
                    }
                }
            }
        }
        $daiyan_sale_where['uid']=session('user_id');
        $daiyan_sale_where['shop_id']=$goods_id;
        $daiyan_sale=M('daiyan')->where($daiyan_sale_where)->find();
        if($daiyan_sale){
            $this->assign('daiyan_sale',$daiyan_sale);
        }
        $shop->where('id='.$goods_id)->setInc('click_num',1);
        $shop_info['shop_sku']=json_decode($shop_info['shop_sku'],true);
//  	dump($shop_info['shop_sku']);
//  	die;
        $this->assign('shop_info',$shop_info);
        //拼团
        if($shop_info['tuanselect']==1){
            $order=M('order');
            $rs_tuan=$order
                ->field('yt_order.order_id,yt_order.buyer_name,yt_order.buyer_mobile,yt_users.user_pic,yt_order.order_id,yt_order.payment_time,yt_order.tuannum,yt_order.buyer_city')
                ->join("yt_order_goods on yt_order_goods.order_id=yt_order.order_id")
                ->join("yt_users on yt_users.id=yt_order.buyer_id")
                ->where("yt_order.tuanselect=1 and yt_order.tuan_is_first_id=0 and yt_order_goods.goods_id=$goods_id and yt_order.order_state='20' and yt_order.refund_state=0" )
                ->group('yt_order.order_id')
                ->select();
            foreach($rs_tuan as $key=>$v){
                $num=get_tuan($v['order_id']);
                $pt_nowtime=time();
                $close_time=$v['payment_time']+60*60*24;
                if($pt_nowtime<$close_time){
                    if($num<$v['tuannum']){
                        $v['num']=$v['tuannum']-$num;
                        $new_rs_tuan[]=$v;
                    }
                }
            }
            $this->assign('new_rs_tuan',$new_rs_tuan);

        }
        //面包屑生成
        $ticket_type=M('ticket_type');
        $bids=explode(',', $shop_info['type_id']);
        $bread[0]['href']='/';
        $bread[0]['name']='首页';
        foreach ($bids as $key => $value) {
            if($key>0){
                $type=$ticket_type->find($value);
                $bread[$key]['href']=U('list/index',array('id' =>$value));
                $bread[$key]['name']=$type['name'];
            }
        }
        $this->assign('bread',$bread);
        //获取相册
        $shop_content=M('shop_content');
        $shop_content_info=$shop_content->where('shop_id='.$goods_id)->find();
        $shop_content_info['config']=json_decode($shop_content_info['config'],true);

        $this->assign('shop_content_info',$shop_content_info);
        $image_array=explode(',',$shop_content_info['gallery']);
        foreach($image_array as $key => $value){
            $arr_img[$key][0]=$value;
            $arr_img[$key][1]=$value;
            $arr_img[$key][2]=$value;
        }
        $arr_img=json_encode($arr_img);
        $this->assign('arr_img',$arr_img);
        //店铺详情
        $store=M('store');
        $store_template=M('store_template');
        $store_info=$store->find($shop_info['store_id']);
        $store_template_info=$store_template->where('store_id='.$shop_info['store_id'])->find();
        $this->assign('store_template_info',$store_template_info);
        $this->assign('store_info',$store_info);
        //店铺所在地
        $address['Province']=getProvinceInfo($store_info['province']);
        $address['City']=getCityInfo($store_info['city']);
        $address['District']=getDistrictInfo($store_info['district']);
        $this->assign('address',$address);
        //销量排行榜
        $sale_num=$shop->where('store_id='.$shop_info['store_id'])->order('sale_num desc')->limit(8)->select();
        //阅读排行榜
        $click_num=$shop->where('store_id='.$shop_info['store_id'])->order('click_num desc')->limit(8)->select();
        $this->assign('sale_num',$sale_num);
        $this->assign('click_num',$click_num);
        $order_goods_evaluate=M('order_goods_evaluate');
        //图片评价
        $img_evaluate_count=$order_goods_evaluate->where("goods_id=$goods_id and  is_show=1 and  image <>''")->count();
        //好评
        $good_evaluate_count=$order_goods_evaluate->where("goods_id=$goods_id and is_show=1 and (scores=5 or scores=4)")->count();
        //差评
        $bad_evaluate_count=$order_goods_evaluate->where("goods_id=$goods_id and  is_show=1 and (scores=1 or scores=2)")->count();
        //中评
        $zhong_evaluate_count=$order_goods_evaluate->where("goods_id=$goods_id and is_show=1 and scores=3")->count();
        $this->assign('img_evaluate_count',$img_evaluate_count);
        $this->assign('good_evaluate_count',$good_evaluate_count);
        $this->assign('bad_evaluate_count',$bad_evaluate_count);
        $this->assign('zhong_evaluate_count',$zhong_evaluate_count);
        //判断收藏
        $users_storecollect=M('users_storecollect');
        $users_shopcollect=M('users_shopcollect');
        if(session('user_id')){
            $newshopinfo['shop_id']=$goods_id;
            $newshopinfo['user_id']=session('user_id');
            $rsshopinfo=$users_shopcollect->where($newshopinfo)->select();
            if($rsshopinfo){
                $rsshopinfo_status=1;
            }else{
                $rsshopinfo_status=0;
            }
            $newstoreinfo['shore_id']=$shop_info['store_id'];
            $newstoreinfo['user_id']=session('user_id');
            $rsstoreinfo=$users_storecollect->where($newstoreinfo)->select();
            if($rsstoreinfo){
                $rsstoreinfo_status=1;
            }else{
                $rsstoreinfo_status=0;
            }

        }else{
            $rsshopinfo_status=0;
            $rsstoreinfo_status=0;
        }

        $this->assign('rsshopinfo_status',$rsshopinfo_status);
        $this->assign('rsstoreinfo_status',$rsstoreinfo_status);
        //获取优惠券
        $coupon_goods=M('coupon_goods');
        $coupon_type_id=$coupon_goods->field('coupon_type_id')->where("goods_id=$goods_id")->group('coupon_type_id')->select();
        foreach($coupon_type_id as $key=>$value){
            if($key==0){
                $coupon_type_ids=$value['coupon_type_id'];
            }else{
                $coupon_type_ids=','.$value['coupon_type_id'];
            }
        }
        $coupon_type=M('coupon_type');
        $map['coupon_type_id']  = array('in',$coupon_type_ids);
        $map['range_type']  = 1;
        $map['_logic'] = 'OR';
        $coupons=$coupon_type->where($map)->select();
        $coupons_i=0;
        foreach($coupons as $key=>$value){
            $start_time=strtotime($value['start_time']);
            $end_time=strtotime($value['end_time']);
            $nowtime=time();
            if($start_time<=$nowtime && $end_time>=$nowtime){
                $new_coupons[$coupons_i]=$value;
                $coupons_i++;
            }
        }
        $shop_count=M('shop')->where('ishidden=0 and store_id='.$shop_info['store_id'])->count();
        $shop_store_count=M('users_storecollect')->where('shore_id='.$shop_info['store_id'])->count();
        $this->assign('shop_count',$shop_count);
        $this->assign('shop_store_count',$shop_store_count);
        $this->assign('coupons',$new_coupons);
        $this->assign('crowdfunding_info',$crowdfunding_info);
        $this->display();
    }



    /**
     * 计算剩余天时分。
     * $unixEndTime string 终止日期的Unix时间
     * @author tangxinzhuan
     * @version 2016-10-28
     */
    private function ShengYu_Tian_Shi_Fen($unixEndTime=0)
    {
        if ($unixEndTime <= time()) { // 如果过了活动终止日期
            return '0天0时0分';
        }

        // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
        $time = $unixEndTime - time();

        $days = 0;
        if ($time >= 86400) { // 如果大于1天
            $days = (int)($time / 86400);
            $time = $time % 86400; // 计算天后剩余的毫秒数
        }

        $xiaoshi = 0;
        if ($time >= 3600) { // 如果大于1小时
            $xiaoshi = (int)($time / 3600);
            $time = $time % 3600; // 计算小时后剩余的毫秒数
        }

        $fen = (int)($time / 60); // 剩下的毫秒数都算作分

        return $days.'天'.$xiaoshi.'时'.$fen.'分';
    }
    public function add_cart(){
        $data=I('post.');
        //print_r($data);
        $crowdfunding=M('crowdfunding');
        $crowdfunding_info=$crowdfunding->find($data['id']);
        $goods_id=$crowdfunding_info['goods_id'];
        $shop_sku=M('shop_sku');
        $shop=M('shop');
        $buygood=I('post.buygood',0);
        $shop_info=$shop->find($goods_id);
        if(isset($data['yanse'])||isset($data['guige'])){
            //有属性
            $map['shop_id']=$goods_id;
            if(isset($data['yanse'])){
                $map['sku_style']=$data['yanse'];
            }
            if(isset($data['guige'])){
                $map['sku_spec']=$data['guige'];
            }
            $sku_info=$shop_sku->where($map)->find();

        }
        //整理添加数据
        $new_cart['shop_id']=$goods_id;
        $new_cart['store_id']=$shop_info['store_id'];
        $new_cart['num']=$data['num'];
        $new_cart['select']=1;
        //$new_cart['recommend_id']=$data['rec_id'];
        $new_cart['tuanselect']=$data['type']?$data['type']:0;
        $new_cart['ctuan_id']=$data['ctuan_id']?$data['ctuan_id']:0;
        if($sku_info){
            $new_cart['sku_id']=$sku_info['sku_id'];
        }else{
            $new_cart['sku_id']=0;
        }
        //判断用户是否登录
        if(session('user_id')){
            $uid=session('user_id');
        }else{
            $uid=0;
        }

        if($uid==0){
            //未登录写入cookie
            $cart_array = cookie('cart_array');
            if(!empty($cart_array)){
                $cart_array = json_decode($cart_array,true);
                $tmp_array = array();
                foreach($cart_array as $k=>$v){
                    $tmp_array[] = $v['cart_id'];
                }
                $cart_id = max($tmp_array) + 1;
                $is_have = true;
                foreach($cart_array as $k=>$v){
                    if($v["shop_id"] ==$new_cart['shop_id'] && $v["sku_id"] == $new_cart['sku_id']){
                        $is_have = false;
                        $cart_array[$k]["num"] = $new_cart["num"] + $v["num"];
                    }
                }
                if($is_have){
                    $data["cart_id"] = $cart_id;
                    $cart_array[] = $new_cart;
                }
            }else{
                $data["cart_id"] = 1;
                $cart_array[] = $new_cart;
            }
            $cart_array_string = json_encode($cart_array);
            try{
                cookie('cart_array', $cart_array_string, 3600);
                $this->ajax_r(1,$new_cart,'成功加入购物车！');
            }catch(\Exception $e){
                $this->ajax_r(0,$e,'加入购物车失败！');
            }
        }else{
            $rs=addcart($uid,$new_cart['shop_id'],$new_cart['sku_id'],$new_cart['num'],$new_cart['tuanselect'],$new_cart['ctuan_id'],$buygood);
            $cart_array = cookie('cart_array');

            if($rs==1){
                $this->ajax_r(1,$rs,'成功加入购物车！');
            }elseif($rs==2){
                $this->ajax_r(0,'','不能参加自己所开的团！');
            }elseif($rs==3){
                $this->ajax_r(0,'','库存不足！');
            }elseif($rs==4){
                $this->ajax_r(0,'','该产品限购！');
            }elseif($rs==5){
                $this->ajax_r(0,'','不可重复参团！');
            }elseif($rs==6){
                $this->ajax_r(0,'','购买量不得小于起拍量！');
            }else{
                $this->ajax_r(0,'','加入购物车失败！');
            }
        }
    }
    public function del_cart(){

        $data_id=I('post.data_id');
        if(is_array($data_id)){
            foreach($data_id as $key=>$val){
                $val=explode(',',$val);
                $shop_id=$val[0];
                $sku_id=$val[1];
                if(session('user_id')){
                    $uid=session('user_id');
                    $cart=M('cart');
                    $rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->delete();
                }else{
                    $cart_array = cookie('cart_array');
                    $cart_array = json_decode($cart_array,true);
                    foreach($cart_array as $key => $v){
                        if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
                            unset($cart_array[$key]);
                        }
                    }
                    $cart_array_string = json_encode($cart_array);
                    cookie('cart_array', $cart_array_string, 3600);
                }
            }
        }else{
            $data_id=explode(',',$data_id);
            $shop_id=$data_id[0];
            $sku_id=$data_id[1];
            if(session('user_id')){
                $uid=session('user_id');
                $cart=M('cart');
                $rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->delete();
            }else{
                $cart_array = cookie('cart_array');
                $cart_array = json_decode($cart_array,true);
                foreach($cart_array as $key => $v){
                    if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
                        unset($cart_array[$key]);
                    }
                }
                $cart_array_string = json_encode($cart_array);
                cookie('cart_array', $cart_array_string, 3600);
            }
        }

        $this->ajax_r(1,'','删除成功');

    }

    public function get_cart_info(){
        $shop=M('shop');
        $shop_sku=M('shop_sku');
        //判断用户是否登录
        $count['num']=0;
        $count['price']=0;
        if(session('user_id')){
            $uid=session('user_id');
            $cart=M('cart');
            $cart_list=$cart->where('buyer_id='.$uid)->select();

            $cart_info;
            foreach($cart_list as $key => $v){

                $cart_info[$key]['num']=$v['num'];
                $cart_info[$key]['price']=$v['price'];
                $cart_info[$key]['shop_picture']=$v['shop_picture'];
                if($v['sku_id']!=0){
                    $rs_shop_sku=$shop_sku->where('sku_id='.$v['sku_id'])->find();
                    $cart_info[$key]['shop_name']=$v['shop_name'].'&nbsp;'.$rs_shop_sku['sku_spec'].'&nbsp;'.$rs_shop_sku['sku_style'];
                }else{
                    $cart_info[$key]['shop_name']=$v['shop_name'];
                }
                $cart_info[$key]['url']=U('shop/index',array('id'=>$v['shop_id']));
                $cart_info[$key]['id']=$v['shop_id'].','.$v['sku_id'];
                $count['num']=$count['num']+$v['num'];
                $count['price']=$count['price']+($v['num']*$v['price']);
            }
        }else{
            $uid=0;
            $cart_info;
            $cart_array = cookie('cart_array');
            $cart_array = json_decode($cart_array,true);
            foreach($cart_array as $key => $v){
                $shop_id=$v['shop_id'];
                $store_id=$v['store_id'];
                $num=$v['num'];
                $sku_id=$v['sku_id'];
                $rs_shop=$shop->find($shop_id);
                if($sku_id!=0){
                    $rs_shop_sku=$shop_sku->where('sku_id='.$sku_id)->find();
                    $cart_info[$key]['shop_name']=$rs_shop['title'].'&nbsp;'.$rs_shop_sku['sku_spec'].'&nbsp;'.$rs_shop_sku['sku_style'];
                    $cart_info[$key]['price']=$rs_shop_sku['sku_price'];
                }else{
                    $cart_info[$key]['shop_name']=$rs_shop['title'];
                    $cart_info[$key]['price']=$rs_shop['price'];
                }
                $cart_info[$key]['num']=$num;
                $cart_info[$key]['shop_picture']=$rs_shop['image'];
                $cart_info[$key]['url']=U('shop/index',array('id'=>$shop_id));
                $cart_info[$key]['id']=$shop_id.','.$sku_id;
                $count['num']=$count['num']+$num;
                $count['price']=$count['price']+($num*$cart_info[$key]['price']);
            }
        }
        if($cart_info){
            $this->ajax_r(1,$cart_info,$count);
        }else{
            $this->ajax_r(0,'',$count);
        }

    }
    public function cart_list(){
        $count['num']=0;
        $count['price']=0;

        if($_POST){
            $shop=M('shop');
            if(session('user_id')){
                $shop_sku=M('shop_sku');
                $uid=session('user_id');
                M('cart')->where("buyer_id=$uid and mian_send=1")->delete();
                $cart=M('cart');
                $cart_list=$cart->where('buyer_id='.$uid)->select();
                $cart_info;
                $result =   array();
                foreach($cart_list as $key => $v){

                    $rs_shop=$shop->find($v['shop_id']);
                    if($v['sku_id']!=0){
                        $rs_shop_sku=$shop_sku->where('sku_id='.$v['sku_id'])->find();
                        $v['sku_spec']=$rs_shop_sku['sku_spec'];
                        $v['sku_style']=$rs_shop_sku['sku_style'];

                    }
                    //代言优惠
                    $shop_id=$v['shop_id'];

                    $invoice=get_web_invoice();
                    $start_time=time()-60*60*24*$invoice['web_diyong'];
                    $daiyan_info=M('daiyan')->where("uid=$uid and shop_id=$shop_id and time>$start_time")->find();

                    if($daiyan_info){
                        $v['daiyan_sale']=$rs_shop['dikouquan'];
                    }else{
                        $v['daiyan_sale']=0;
                    }
                    $v['url']=U('shop/index',array('id'=>$v['shop_id']));
                    $v['id']=$v['shop_id'].','.$v['sku_id'];
                    $v['oprice']=$rs_shop['oprice'];
                    if($v['select']==1){
                        $count['num']=$count['num']+$v['num'];
                        $count['price']=$count['price']+($v['num']*$v['price'])-($v['num']*$v['daiyan_sale']);;
                    }

                    $result[$v['store_id']][] =$v;
                }
            }else{
                $uid=0;
                $cart_info;
                $store=M('store');
                $cart_array = cookie('cart_array');
                $cart_array = json_decode($cart_array,true);

                $result =   array();
                $shop_sku=M('shop_sku');
                foreach($cart_array as $key => $v){
                    $shop_id=$v['shop_id'];
                    $store_id=$v['store_id'];
                    $store_info=$store->find($store_id);
                    $v['store_name']=$store_info['title'];
                    $num=$v['num'];
                    $sku_id=$v['sku_id'];
                    $rs_shop=$shop->find($shop_id);
                    if($sku_id!=0){
                        $rs_shop_sku=$shop_sku->where('sku_id='.$sku_id)->find();
                        $v['sku_spec']=$rs_shop_sku['sku_spec'];
                        $v['sku_style']=$rs_shop_sku['sku_style'];
                    }
                    $v['daiyan_sale']=0;
                    $v['shop_picture']=$rs_shop['image'];
                    $v['shop_name']=$rs_shop['title'];
                    $v['price']=$rs_shop['price'];
                    $v['oprice']=$rs_shop['oprice'];
                    $v['url']=U('shop/index',array('id'=>$shop_id));
                    $v['id']=$shop_id.','.$sku_id;

                    if($v['select']==1){

                        $count['num']=$count['num']+$num;
                        $count['price']=$count['price']+($v['num']*$v['price']);
                    }



                    $result[$v['store_id']][] =$v;
                }
            }
            if($result){

                $this->ajax_r(1,$result,$count);
            }else{
                $this->ajax_r(0,'','购物车空');
            }
        }else{
            $this->display();
        }

    }

    function edit_cart(){
        if($_POST){
            $data=I('post.');
            $cart=M('cart');
            $goods_number=$data['goods_number'];
            $goods_number=explode(',',$goods_number);
            $shop_id=$goods_number[0];
            $sku_id=$goods_number[1];
            $shop_info=M('shop')->find($shop_id);
            //检测限购  与起拍量
            if($data['type']==1){
                $num=$data['num']+1;
            }
            if($data['type']==2){
                $num=$data['num']-1;
            }
            if($shop_info['xz_num']>0){
                if($num>$shop_info['xz_num']){
                    $this->ajax_r(0,'','该产品限购');
                }
            }
            if($shop_info['qp_num']>0){
                if($num<$shop_info['qp_num']){
                    $this->ajax_r(0,'','购买量不能少于起拍量');
                }
            }


            if($num>0){
                if(session('user_id')){
                    $rs=$cart->where('shop_id='.$shop_id.' and sku_id='.$sku_id.' and buyer_id='.session('user_id'))->setField('num',$num);
                }else{
                    $cart_array = cookie('cart_array');
                    $cart_array = json_decode($cart_array,true);
                    foreach($cart_array as $k=>$v){
                        if($v['shop_id']==$shop_id&& $v['sku_id'] ==$sku_id){
                            $cart_array[$k]['num']=$num;
                        }
                    }
                    $cart_array_string = json_encode($cart_array);
                    cookie('cart_array', $cart_array_string, 3600);
                    $rs=true;
                }
                if($rs){
                    $this->ajax_r(1,'','购物车更新成功');
                }else{
                    $this->ajax_r(0,'','购物车无更新');
                }

            }else{
                $this->ajax_r(0,'','购物车无更新');
            }
        }
    }

    function count_cart(){
        $data_id=I('post.data_id');
        $cart=M('cart');

        if(is_array($data_id)){
            $this->un_select();
            foreach($data_id as $key=>$val){
                $val=explode(',',$val);
                $shop_id=$val[0];
                $sku_id=$val[1];
                if(session('user_id')){
                    $uid=session('user_id');
                    $rs=$cart->where('buyer_id='.$uid.' and shop_id='.$shop_id.' and sku_id='.$sku_id)->setField('select',1);
                }else{
                    $cart_array = cookie('cart_array');
                    $cart_array = json_decode($cart_array,true);
                    foreach($cart_array as $key => $v){
                        if($v['sku_id']==$sku_id&&$v['shop_id']==$shop_id){
                            $cart_array[$key]['select']=1;
                        }
                    }
                    $cart_array_string = json_encode($cart_array);
                    cookie('cart_array', $cart_array_string, 3600);
                }
            }
        }else{
            $this->un_select();
        }
        $this->ajax_r(1,'','购物车更新成功');
    }

    function store_select(){
        $data=I('post.');
        $cart=M('cart');
        $store_id=$data['store_id'];
        $checked=$data['checked'];
        if(session('user_id')){
            $uid=session('user_id');
            $rs=$cart->where('buyer_id='.$uid.' and store_id='.$store_id)->setField('select',$checked);
        }else{
            $cart_array = cookie('cart_array');
            $cart_array = json_decode($cart_array,true);
            foreach($cart_array as $key => $v){
                if($v['store_id']==$store_id){
                    $cart_array[$key]['select']=$checked;
                }
            }
            $cart_array_string = json_encode($cart_array);
            cookie('cart_array', $cart_array_string, 3600);
        }


        $this->ajax_r(1,'','购物车更新成功');
    }

    public function un_select(){
        //全部取消选中
        $cart=M('cart');
        if(session('user_id')){
            $cart->where('buyer_id='.session('user_id'))->setField('select',0);
        }else{
            $cart_array = cookie('cart_array');
            $cart_array = json_decode($cart_array,true);
            foreach($cart_array as $key => $v){
                $cart_array[$key]['select']=0;
            }
            $cart_array_string = json_encode($cart_array);
            cookie('cart_array', $cart_array_string, 3600);
        }
    }

    function pay_sub(){
        $cart=M('cart');
        if(session('user_id')){
            $cart_list=$cart->field('cart_id')->where('buyer_id='.session('user_id').' and  `select`=1')->select();
            session('cart_list_id',$cart_list);

            $this->ajax_r('1','','商品获取成功，正在下单....');
        }else{
            $this->ajax_r('2','','未登录....');
        }
    }
   

}
